package com.ruoyi.flowable.listener.scrap;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquAsset;
import com.ruoyi.equ.domain.EquScrap;
import com.ruoyi.equ.service.IEquScrapService;
import com.ruoyi.flowable.service.IWorkOrderAssetRelService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 报废-驳回监听
 */
@Slf4j
@Component("scrapRejectListener")
public class ScrapRejectListener implements ExecutionListener {
    private static final IEquScrapService scrapService = SpringUtils.getBean(IEquScrapService.class);
    private static final IWorkOrderAssetRelService workOrderAssetRelService = SpringUtils.getBean(IWorkOrderAssetRelService.class);

    @Override
    public void notify(DelegateExecution execution) {
        //创建者
        String createBy = SecurityUtils.getLoginUser().getUser().getNickName();

        //资产状态
        EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);
        equScrap.setCreateBy(createBy);

        //新增
        scrapService.insertEquScrap(equScrap);

        //更新工单状态(流程实例和资产结合的工单)
        String processInstanceId = execution.getProcessInstanceId();
        workOrderAssetRelService.updateWorkOrderStatus(processInstanceId, "已驳回", new Date());
    }
}
